package calcsdk.nw;

import java.util.Arrays;
import java.util.List;

/**
 *
 * @author Develop
 */
public class Util {

    public static float[][] arrayCopy(float[][] arr) {
        float[][] ret = new float[arr.length][arr[0].length];// TODO: зачем 2 раза создавать? (1)
        for (int i = 0; i < arr[0].length; i++) {
            int len = arr[i].length;
            float cpy[] = new float[len];// TODO: и тут тоже?(2)
            cpy = Arrays.copyOf(arr[i], len);// (2)
            ret[i] = cpy;// (1)
        }
        return ret;
    }

    public static int[] arrayDeleteI(int[] arr, int delI) {
        int w = arr.length;
        int[] ret = Arrays.copyOf(arr, w - 1);
        for (int i = delI + 1; i < w; i++) {
            ret[i - 1] = arr[i];
        }
        return ret;
    }

    public static float[] arrayDeleteI(float[] arr, int delI) {
        int w = arr.length;
        float[] ret = Arrays.copyOf(arr, w - 1);
        for (int i = delI + 1; i < w; i++) {
            ret[i - 1] = arr[i];
        }
        return ret;
    }

    public static float[][] arrayDeleteIJ(float[][] arr, int delI, int delJ) {
//        int w=arr[0].length, h=arr.length;
//        float[][] ret = new float[h-1][w-1];
//        
//        for (int j=0;j<delI;j++)
//            ret[j]=Arrays.copyOf(arr[j], w-1);
//        for (int j=delI+1;j<h;j++)
//            ret[j-1]=Arrays.copyOf(arr[j], w-1);
//        
//        for (int j=0;j<delI;j++) 
//            for (int i=delJ+1;i<w;i++)
//                ret[j][i-1]=arr[i][j];
//        for (int j=delI+1;j<h;j++)
//            for (int i=delJ+1;i<w;i++)
//                ret[j][i-1]=arr[i][j];
        int h = arr.length;
        float[][] ret = new float[h - 1][];
        for (int j = 0; j < delI; j++) {
            ret[j] = arrayDeleteI(arr[j], delJ);
        }
        for (int j = delI + 1; j < h; j++) {
            ret[j - 1] = arrayDeleteI(arr[j], delJ);
        }

        return ret;
    }

    public static int arraySearch(int[] arr, int val) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == val) {
                return i;
            }
        }
        return -1;
    }

    public static float[] arrayGetRow(float[][] arr, int getI) {
        return Arrays.copyOf(arr[getI], arr[getI].length);
    }

    public static float[] arrayGetColumn(float[][] arr, int getJ) {
        float[] ret = new float[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i][getJ];
        }
        return ret;
    }

    public static float[][] arrayAddRowColumn(float[][] arr, float[] row, float[] column) {
        int width = arr[0].length;
        int height = arr.length;
        float[][] ret = new float[height + 1][];
        for (int i = 0; i < height; i++) {
            ret[i] = Arrays.copyOf(arr[i], width + 1);
            ret[i][width] = column[i];
        }
        ret[height] = Arrays.copyOf(row, width + 1);
        ret[height][width] = Float.NaN;
        return ret;
    }

    public static int[] arrayAddElem(int[] arr, int elem) {
        int[] ret = Arrays.copyOf(arr, arr.length + 1);
        ret[arr.length] = elem;
        return ret;
    }

    public static boolean arrayAllNanExceptI(float[] arr, int eI) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == arr[i] && i != eI) {
                return false;
            }
        }
        return true;
    }

    public static int[] buildIntArray(List<Integer> integers) {
        int[] ints = new int[integers.size()];
        int i = 0;
        for (Integer n : integers) {
            ints[i++] = n;
        }
        return ints;
    }
	
	public static float[][] arrayTranspose(float arr[][]) {
		int w=arr.length, h=arr[0].length;
		float ret[][]=new float[h][w];
		
		for (int i=0;i<w;i++) {
			for (int j=0;j<h;j++) {
				ret[i][j]=arr[j][i];
			}
		}
		
		return ret;
	}

    public static void arrayPrint(int arr[]) {
        String str = "[";
        for (int i = 0; i < arr.length - 1; i++) {
            str += arr[i] + ", ";
        }
        str += arr[arr.length - 1] + "]";
        System.out.println(str);
    }

    public static void arrayPrint(float arr[]) {
        String str = "[";
        for (int i = 0; i < arr.length - 1; i++) {
            str += arr[i] + ", ";
        }
        str += arr[arr.length - 1] + "]";
        System.out.println(str);
    }
	
	
	public static String arrayToString(float mas[][], int nameH[], int nameV[], int nameAdd) {
		if (mas == null) {
			return "Матрица пустая\r\n";
		}
		String res="\t";
		for (int i = 0; i < mas[0].length; i++) {
			res+=((nameH==null ? i : nameH[i])+nameAdd) + "\t";
		}
		res+="\r\n";
		
		res+="\t";
		for (int i = 0; i < mas[0].length; i++) {
			res+="__\t";
		}
		res+="\r\n";
		
		for (int i = 0; i < mas.length; i++) {
			res+=((nameV==null ? i : nameV[i])+nameAdd) + "|\t";
			for (int j = 0; j < mas[i].length; j++) {
				res+=mas[i][j] + "\t";
			}
			res+="\r\n";
		}
		return res;
	}
    public static String arrayToString(float mas[][]) {
        return arrayToString(mas, null, null,0);
    }
    public static String arrayToString(float mas[][], int nameAdd) {
        return arrayToString(mas, null, null,nameAdd);
    }
	
	public static String arrayToString(int mas[][], int nameH[], int nameV[], int nameAdd) {
		if (mas == null) {
			return "Матрица пустая\r\n";
		}
		String res="\t";
		for (int i = 0; i < mas[0].length; i++) {
			res+=((nameH==null ? i : nameH[i])+nameAdd) + "\t";
		}
		res+="\r\n";
		
		res+="\t";
		for (int i = 0; i < mas[0].length; i++) {
			res+="__\t";
		}
		res+="\r\n";
		
		for (int i = 0; i < mas.length; i++) {
			res+=((nameV==null ? i : nameV[i])+nameAdd) + "|\t";
			for (int j = 0; j < mas[i].length; j++) {
				res+=mas[i][j] + "\t";
			}
			res+="\r\n";
		}
		return res;
	}
    public static String arrayToString(int mas[][]) {
        return arrayToString(mas, null, null,0);
    }
    public static String arrayToString(int mas[][], int nameAdd) {
        return arrayToString(mas, null, null,nameAdd);
    }
    
	
    public static String arrayToString(float arr[]) {
        String res="[";
        for (int i=0;i<arr.length-1;i++)
            res+=arr[i]+"\t| ";
        res+=arr[arr.length-1]+"\t]";
        return res;
    }
    
    /**
     *
     * @param dt - System.currentTimeMillis() / Date().getTime();
     * @param dtx - System.currentTimeMillis() / Date().getTime();
     * @return Разница во времени
     */
    public static String getDiffTime(long dt, long dtx) {
        String nm[] = {"Мс.", "Сек.", "Мин.", "Час.", "Дн.", "Нед.", "Мес.", "Лет", "Век."};
        double tm[] = {0.001, 1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600};
        double dif = Math.abs(dt - dtx);
        if (dif < 1) {
            return "0 " + nm[0];
        }
        //double d = dt/1000;
        double x = 0;
        String tmp = "";
        for (int i = tm.length - 1; i >= 0; i--) {
            if (dif >= tm[i] * 1000) {
                x = Math.floor(dif / (tm[i] * 1000));
                dif -= (x * tm[i] * 1000);
                if (Math.floor(x) != 0) {
                    tmp += (int) x + "-" + nm[i] + " ";
                }
            }
        }
        return tmp.trim();
    }
	
	public static String arrayToJavaCode(float mas[][]) {
		String res="float[][] mas={\r\n";
		
		for (int i = 0; i < mas.length; i++) {
			res+="{";
			int j=0;
			for (j = 0; j < mas[i].length-1; j++) {
				res+=(mas[i][j]==mas[i][j] ? mas[i][j]+"f" : "Float.NaN") + ", ";
			}
			res+=(mas[i][j]==mas[i][j] ? mas[i][j]+"f" : "Float.NaN") + "},\r\n";
		}
		return res+"};\r\n";
	}
	
	public static float getPathLen(float[][] mas, int[] path) {
		float len=0;
		for (int i=0;i<path.length-1;i++) {
			//if (mas[path[i]][path[i+1]]!=mas[path[i]][path[i+1]]) System.out.println(path[i]+" "+path[i+1]+" ");
			len+=mas[path[i]][path[i+1]];
		}
		return len;
	}
	
	
	public static int[] arrayInvert(int[] arr) {
		int[] ret=new int[arr.length];
		for (int i=0;i<arr.length;i++) ret[i]=arr[arr.length-i-1];
		return ret;
	}
}
